<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/7/20
 * Time: 11:01
 */

namespace app\command\company\data_change;

use app\company\model\CompanyOrderDetail;
use app\company\model\WarehouseStorage;
use app\mini\model\StoreOrderDetail;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class AddGoodsCateId extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:3')->setDescription('从采购-入库-销售 整个声明周期 记录分类id');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('从采购-入库-销售 整个声明周期 记录分类id开始');
        $model = new \app\mini\model\CompanyOrderStorage();
        $model->startTrans();
        try {
            //更新采购明细表
            CompanyOrderDetail::select()->each(function ($item){
                $goods_cate_id = $item->goods()->withTrashed()->value('goods_cate_id');
                $item->goods_cate_id = $goods_cate_id;
                $item->save();
            });
            //更新库存明细
            \app\company\model\CompanyOrderStorage::select()->each(function ($item){
                $goods_cate_id = $item->goods()->withTrashed()->value('goods_cate_id');
                $item->goods_cate_id = $goods_cate_id;
                $item->save();
            });
            //更新销售明细
            StoreOrderDetail::select()->each(function ($item){
                $goods_cate_id = $item->goods()->withTrashed()->value('goods_cate_id');
                $item->goods_cate_id = $goods_cate_id;
                $item->save();
            });
            //更新出入库记录
            \app\company\model\WarehouseStorageGoodsLogs::select()->each(function ($item){
                $goods_cate_id = $item->goods()->withTrashed()->value('goods_cate_id');
                $item->goods_cate_id = $goods_cate_id;
                $item->save();
            });
            $model->commit();
            $output->writeln('从采购-入库-销售 整个声明周期 记录分类id成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('从采购-入库-销售 整个声明周期 记录分类id失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}